package com.flymobi.service;

import java.util.List;

import com.flymobi.fw.modules.CommonService;
import com.flymobi.po.NewsImage;
import com.flymobi.po.Pindao;
import com.flymobi.util.QL;

public class NewsImageService extends CommonService {

	public List<NewsImage> list(Long newsId, int startRow, int pagePerSize) {
		return dao.find(NewsImage.class, QL.HQL.NewsImage.LIST,
				new Object[] { newsId }, startRow, pagePerSize);
	}

	public List<NewsImage> list(Long newsId, Long idSeq, int startRow,
			int pagePerSize) {
		return dao.find(NewsImage.class, QL.HQL.NewsImage.LIST_ALL,
				new Object[] { newsId, idSeq }, startRow, pagePerSize);
	}

	public Long getCount(Long newsId) {
		return dao
				.getCount(QL.HQL.NewsImage.GET_COUNT, new Object[] { newsId });
	}

	public void save(NewsImage NewsImage) {
		dao.save(NewsImage);
	}

	public NewsImage load(String id) {
		return dao.load(NewsImage.class, new Long(id));
	}

	public NewsImage get(String id) {
		return dao.get(NewsImage.class, new Long(id));
	}

	public void delete(String id) {
		dao.delete(this.get(id));
	}

	public void updateUp(String id) {
		NewsImage pe = this.get(id);
		NewsImage peUp = findUp(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}

	public void updateDown(String id) {
		NewsImage pe = this.get(id);
		NewsImage peUp = findDown(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}

	private void switchPosion(NewsImage pe, NewsImage peUp) {
		long pe1idx = pe.getIdx();
		pe.setIdx(peUp.getIdx());
		peUp.setIdx(pe1idx);
		dao.save(pe);
		dao.save(peUp);
	}

	private NewsImage findUp(NewsImage pe) {
		return dao.findSingle(NewsImage.class, QL.HQL.NewsImage.LIST_UP,
				new Object[] { pe.getNewsId(),pe.getIdx() });
	}

	private NewsImage findDown(NewsImage pe) {
		return dao.findSingle(NewsImage.class, QL.HQL.NewsImage.LIST_DOWN,
				new Object[] {pe.getNewsId(),pe.getIdx() });
	}

	public Long getSeq(Long newsId) {
		return dao.getSeq(QL.HQL.NewsImage.GET_MAX_IDX, new Object[] {newsId});
	}
}
